import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";
import { GameConfig } from "../../src/config/GameConfig";
import DateUtil from "../../src/engine/utils/DateUtil";
import { AccountDailyLogin } from "./AccountDailyLogin";
import { AccountDailyRegister } from "./AccountDailyRegister";
import { Repository } from "typeorm";
import { AccountEntity } from "../entity/AccountEntity";
import { DBSourceManager } from "../../src/engine/utils/DBSourceManager";

export class AccountDao extends DaoBase {

    public static Tb_Name: string = "t_accounts";


    public static async getRepository() {
        let repository: Repository<AccountEntity> = await this.getRepositoryBase(AccountEntity, this.getAccountDbName())
        return repository;
    }

    public static async getAccountNum() {
        let repository: Repository<AccountEntity> = await this.getRepository()
        let count: number = await repository.count()
        return count
    }

    public static async get_account_list(pagenum: number, pagesize: number) {
        let repository: Repository<AccountEntity> = await this.getRepository()
        let start: number = (pagenum - 1) * pagesize
        let sql: string = "select * from  " + this.Tb_Name + " limit ?,?"
        let sqlParams = [start, pagesize];
        let results = await DBSourceManager.instance.query(repository, sql, sqlParams)
        return results
    }

    public static async search_account(searchKey: string, searchValue: string) {
        let repository: Repository<AccountEntity> = await this.getRepository()
        let sql: string = "select * from  " + this.Tb_Name + "  where " + searchKey + " = ?"
        let sqlParams = [searchValue];
        let results = await DBSourceManager.instance.query(repository, sql, sqlParams)
        return results
    }

    public static async is_ip_exist(ip: string) {
        let repository: Repository<AccountEntity> = await this.getRepository()
        let result: Array<any> = await repository.findAndCountBy({
            ip: ip,
            account_type: GameConfig.Account_Type.Account
        })
        if (result[1] >= 1) {
            return true
        }
        return false
    }

    public static async get_account_info(account: string): Promise<AccountEntity> {
        let repository: Repository<AccountEntity> = await this.getRepository()
        let result: AccountEntity = await repository.findOneBy({ account: account })
        return result
    }

    public static async update_account(entity: AccountEntity) {
        let repository: Repository<AccountEntity> = await this.getRepository()
        let result = repository.save(entity)
        return result
    }


    public static async checkAddDailyLogin(date: Date) {
        let startDate: Date = new Date(date.getFullYear(), date.getMonth(), date.getDate())
        let endDate: Date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1)
        let startTime: string = DateUtil.formatDateStr(startDate.getTime())
        let endTime: string = DateUtil.formatDateStr(endDate.getTime())
        let repository: Repository<AccountEntity> = await this.getRepository()
        let sql: string = "select account from " + this.Tb_Name + " WHERE last_login_time >= ? and last_login_time < ?  ";
        let sqlParams = [startTime, endTime];
        let results = await repository.query(sql, sqlParams)
        // Logger.log("checkAddDailyLogin=", sql, sqlParams,  results)
        let loginDate: string = DateUtil.formatDateStr3(date.getTime())
        for (let i = 0; i < results.length; i++) {
            AccountDailyLogin.insertAccountDailyLogin(loginDate, results[i]["account"])
        }
    }

    public static async checkAddDailyRegister(date: Date) {
        let startDate: Date = new Date(date.getFullYear(), date.getMonth(), date.getDate())
        let endDate: Date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1)
        let startTime: string = DateUtil.formatDateStr(startDate.getTime())
        let endTime: string = DateUtil.formatDateStr(endDate.getTime())
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getAccountDbName());
        let sql: string = "select account from " + this.Tb_Name + " WHERE create_time >= ? and create_time < ?  ";
        let sqlParams = [startTime, endTime];
        let results = await client.queryPromise(sql, sqlParams);
        // Logger.log("checkAddDailyLogin=", sql, sqlParams,  results)
        let registerDate: string = DateUtil.formatDateStr3(date.getTime())
        for (let i = 0; i < results.length; i++) {
            AccountDailyRegister.insertAccountDailyRegister(registerDate, results[i]["account"])
        }
    }



}